package edu.mis.modules.goods;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import edu.mis.modules.categoryOfGoods.CategoryOfGoodsService;

public class GoodsValidator extends Validator {

	@Override
	protected void validate(Controller c) {
		validateString("name", 1, 30, "name", "商品名称长度不能超过30");
		validateRequired("name", "name", "商品名称不能为空");
		validateDouble("price", "price", "非法的价格");
		validateInteger("categoryId", "category", "非法的商品类别");
		Integer categoryId = c.getParaToInt("categoryId");
		if (categoryId != null && new CategoryOfGoodsService().findCategoryById(categoryId) == null)
			addError("category", "非法的商品类别");
	}

	@Override
	protected void handleError(Controller c) {
		c.setAttr("result", false);
		c.renderJson();
	}

}
